package edu.upc.fib.lamsandroid.client.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Point;
import android.util.TypedValue;
import android.view.Display;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

public class HelpDialog {

    private final Context context;

    public HelpDialog(Context context) {
        this.context = context;
    }

    public void show(String title, String body) {
        final AlertDialog dialog = new AlertDialog.Builder(context).create();
        dialog.setCanceledOnTouchOutside(true);
        dialog.setTitle(title);
        dialog.setMessage(body);

        // get screen size
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Point screenSize = new Point();
        display.getSize(screenSize);

        // get actionbar height
        int actionBarHeight = 0;
        TypedValue tv = new TypedValue();
        if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
            actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
        }

        Window window = dialog.getWindow();
        WindowManager.LayoutParams wmlp = window.getAttributes();
        wmlp.width = screenSize.x/3;
        wmlp.height = 300;//screenSize.y - 2*actionBarHeight;
        wmlp.gravity = Gravity.TOP | Gravity.LEFT;
        wmlp.x = screenSize.x - wmlp.width;
        wmlp.y = 0;

        dialog.show();
    }
}
